<?php
namespace app\portal\widget;

use think\Db;

class WuyeWidget{
//    公告
    public function notice($table,$cateId,$url){
        $user_id = cookie('user_id');
        $app_id = cookie('app_id');
        $where = ['user_id'=>$user_id,'cate_id'=>$cateId,'app_id'=>$app_id];
        $li = '';
        if(!empty($user_id)){
            $list = Db::name($table)->where($where)->order('time desc')->select();
            if(!empty($list)){
                foreach ($list as $k => $v){
                    if($v['cate_id'] == 16){
                        $li .= '<li class="mui-table-view-cell mui-media">
                                <a class="mui-navigate-right" href="'.$url.'?id='.$v['id'].'">
                                    <div class="mui-media-body"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-tishi"></use></svg>
                                        '.date("Y-m-d H:i",$v['time']).'
                                        <p class=\'mui-ellipsis\'>'.$v['desc'].'</p>
                                    </div>
                                </a>
                            </li>';
                    }elseif($v['cate_id'] == 17){
                        $li .= '<div class="mui-card">
                                    <div class="mui-card-header">'.$v['title'].'</div>
                                    <div class="mui-card-content">
                                        <div class="mui-card-content-inner">
                                            '.htmlspecialchars_decode($v['content']).'
                                        </div>
                                    </div>
                                    <div class="mui-card-footer">时间：'.date('Y-m-d H:i',$v['time']).'</div>
                                </div>';
                    }else{
                        $li .= '<li class="mui-table-view-cell mui-media">
                                <a class="mui-navigate-right" href="'.$url.'?id='.$v['id'].'">
                                    <div class="mui-media-body">
                                        '.$v['title'].'
                                        <p class=\'mui-ellipsis\'>'.$v['desc'].'</p>
                                    </div>
                                </a>
                            </li>';
                    }

                }
            }
        }
        echo $li;

    }
//    租赁列表
    public function lease($cateName){
        $user_id = cookie('user_id');
        $li = '';
        $cate = Db::table('wy_portal_post')
            ->alias('a')
            ->join('portal_category_post b','a.id=b.post_id','left')
            ->join('portal_category c','b.category_id=c.id','left')
            ->where(['a.user_id'=>$user_id,'c.name'=>$cateName])
            ->field('a.*,c.name as cateName')
            ->select();
        foreach ($cate as $v){
            $more = json_decode($v['more']);
            $li .= '<li class="mui-table-view-cell mui-media">
                            <a href="leasedetail?id='.$v['id'].'">
                                <img class="mui-media-object mui-pull-left" src="'.cmf_get_image_url($more->thumbnail).'">
                                <div class="mui-media-body">
                                    <span>'.$v['post_title'].'</span>
                                    <p class="mui-ellipsis" style="margin-top: 5px;">'.$v['post_excerpt'].'</p>
                                    <p class="mui-ellipsis" style="margin-top: 5px;">'.$v['cateName'].'&nbsp;&nbsp;&nbsp;&nbsp;发布时间：'.date('Y-m-d',$v['create_time']).'</p>
                                </div>
                            </a>
                        </li>';
        }
        echo $li;
    }
//    租赁详情
    public function leasedetail($table,$id){
        $where = ['id'=>$id];
        $li = '';
        if(!empty($id)){
            $list = Db::name($table)->where($where)->order('update_time desc')->find();
            $imgSrc = json_decode($list['more'])->thumbnail;
            if(!empty($list)){
                $li .= '<div class="mui-card">
                            <div class="mui-card-header mui-card-media">
                               <img class="mui-media-object mui-pull-left" src="'.cmf_get_image_url($imgSrc).'">
                                <div class="mui-media-body">
                                    '.$list['post_title'].'
                                    <p>发表于 '.date('Y-m-d H:i',$list['create_time']).'</p>
                                </div>
                            </div>
                            <div class="mui-card-content-inner">
                                '.htmlspecialchars_decode($list['post_content']).'
                            </div>
                        </div>';
            }
            echo $li;
        }
    }
//    报修
    public function baoxiu($table){
        $user_id = cookie('user_id');
        $list = Db::name($table)->where('user_id',$user_id)->select();
        if(!empty($list)){
            $option = '';
            foreach ($list as $v){
                $option .= '<option value="'.$v['id'].'">'.$v['name'].'</option>';
            }
        }
        echo $option;
    }
//    个人信息
    public function info(){
        $where['openid'] = cookie('openid');
        $where['user_id'] = cookie('user_id');
        $info = Db::name('wx_fans')->where($where)->find();
        $li = '';
        if($info){
            if(!empty($info['remark'])){
                $li = '<div class="mui-card-header mui-card-media" style="height: 100px;">
                        <img src="'.$info['headimgurl'].'" style="height: 80px;width: 80px;">
                        <div class="mui-media-body" style="line-height: 30px; font-size: 20px;padding-left: 3em;padding-top: 0.5em;">
                            '.$info['remark'].'
                            <p>手机号：'.$info['phone'].'</p>
                        </div>
                    </div>';
            }else{
                $li = '<div class="mui-card-header mui-card-media" style="height: 100px;">
                        <img src="'.$info['headimgurl'].'" style="height: 80px;width: 80px;">
                        <div class="mui-media-body" style="line-height: 30px; font-size: 20px;padding-left: 3em;padding-top: 0.5em;">
                            '.$info['nickname'].'
                            <p>手机号：'.$info['phone'].'</p>
                        </div>
                    </div>';
            }
        }
        echo $li;
    }
//    报修
    public function baoxiudan($table,$url){
        $user_id = cookie('user_id');
        $openid = cookie('openid');
        $where = ['user_id'=>$user_id,'openid'=>$openid];
        $li = '';
        if(!empty($user_id)){
            $list = Db::name($table)->where($where)->order('time desc')->select();
            if(!empty($list)){
                foreach ($list as $k => $v){
                    $li .= '<li class="mui-table-view-cell mui-media">
                                <a class="mui-navigate-right" href="'.$url.'?id='.$v['id'].'">
                                    <div class="mui-media-body">
                                        <p class="mui-ellipsis">'.date("Y-m-d H:i",$v['time']).'</p>
                                        <p style="color:#000;font-size: 18px;">'.$v['desc'].'</p>
                                    </div>
                                </a>
                            </li>';
                }
            }
        }
        echo $li;

    }
//    报修单
    public function baoxiudandetail($table,$id){
        $where = ['id'=>$id];
        $li = '';
        if(!empty($id)){
            $list = Db::name($table)->where($where)->order('time desc')->find();
            $imgrc = explode(',',$list['imgsrc']);
            $img = '';
            foreach ($imgrc as $v){
                $img .= '<img src="'.cmf_get_image_url($v).'" alt="" width="30%" style="margin-left: 3.33%" />';
            }
            if(!empty($list)){
                $li .= '<div class="mui-card-content">
                                <div class="mui-card-content-inner">
                                    <p>'.date("Y-m-d H:i",$list['time']).'</p>
                                    <p style="color: #333;">'.$list['desc'].'</p>
                                </div>
                                '.$img.'
                            </div>';
            }
        }
        echo $li;

    }
//    社区活动
    public function active($cateName,$status){
        $ending = $status;
        $user_id = cookie('user_id');
        $cate = Db::name('portal_category')->where('name',$cateName)->field('id')->find();
        $cateId = $cate['id'];
        $articleId = Db::name('portal_category_post')->where(['category_id'=>$cateId,'status'=>1])->order('id desc')->field('post_id')->select();
        $li = '';
        if(!empty($articleId)){
            foreach ($articleId as $v){
                $article = Db::name('portal_post')->where(['id'=>$v['post_id'],'ending'=>$ending,'user_id'=>$user_id])->find();
                if(!empty($article) && $ending == '1'){
                    $more = json_decode($article['more']);
                    $li .= '<div class="mui-card">
                                <div class="mui-card-content" >
                                    <img src="'.cmf_get_image_url($more->thumbnail).'" alt="" width="100%" height="200px"/>
                                    <p class="mui-slider-title">'.$article['post_title'].'</p>
                                </div>
                                <div class="mui-card-footer">
                                    <a class="mui-card-link">在线报名进行中</a>
                                    <a href="http://'.$_SERVER['HTTP_HOST'].'/portal/wuye/signUp/id/'.$article['id'].'" class="mui-card-link mui-btn mui-btn-warning mui-btn-outlined singup" style="height: 25px;line-height: 8px;background-color: #f0ad4e;color: #fff;">报名</a>
                                </div>
                            </div>';
                }
            }
            echo $li;
        }
    }
    public function activeEnding($cateName,$status){
        $ending = $status;
        $user_id = cookie('user_id');
        $cate = Db::name('portal_category')->where('name',$cateName)->field('id')->find();
        $cateId = $cate['id'];
        $articleId = Db::name('portal_category_post')->where(['category_id'=>$cateId,'status'=>1])->order('id desc')->field('post_id')->select();
        $li = '';
        if(!empty($articleId)){
            foreach ($articleId as $v){
                $article = Db::name('portal_post')->where(['id'=>$v['post_id'],'ending'=>$ending,'user_id'=>$user_id])->find();
                if(!empty($article) && $ending == '0'){
                    $more = json_decode($article['more']);
                    $li .= '<div class="mui-card">
                                <div class="mui-card-content" >
                                    <img src="'.cmf_get_image_url($more->thumbnail).'" alt="" width="100%" height="200px"/>
                                    <p class="mui-slider-title">'.$article['post_title'].'</p>
                                </div>
                                <div class="mui-card-footer">
                                    <a class="mui-card-link"></a>
                                    <a class="mui-card-link mui-btn mui-btn-warning mui-btn-outlined" style="height: 25px;line-height: 8px;background-color: #f0ad4e;color: #fff;" href="http://'.$_SERVER['HTTP_HOST'].'/portal/wuye/activedetail/id/'.$article['id'].'">场景回顾</a>
                                </div>
                            </div>';
                }
            }
            echo $li;
        }
    }
//    朋友圈回复
    public function huifu($friend_id =''){
        $data = Db::name('wy_friend_huifu')->where('friend_id',$friend_id)->order('time desc')->select();
        $sender = Db::name('wy_friend')->where('id',$friend_id)->find();
        $senderNickname = Db::name('wx_fans')->where('openid',$sender['openid'])->find();
        $li = '';
        if(!empty($data)){
            foreach ($data as $k => $v){
                $headimgurl = Db::name('wx_fans')->where('openid',$v['hf_openid'])->find();
                $hfId = $v['id'].$v['time'];
                $hfIdBtn = $v['id'].$v['time'].$v['id'];
                $hfIdQueren = $v['id'].$v['time'].$v['id'].$v['time'];
                $url = "{:url('portal/wuye/huifu')}";
                $li .= '<li class="mui-table-view-cell mui-media" id="'.$hfId.'">
                        <a href="javascript:;">
                            <img class="mui-media-object mui-pull-left" src="'.$headimgurl['headimgurl'].'">
                            <div class="mui-media-body">
                            '.$v['content'].'
                                <p class="mui-ellipsis" style="font-size: 12px;">'.$headimgurl['nickname'].'@'.$senderNickname['nickname'].'</p>
                            </div>
                        </a>
                    </li><div class="mui-input-row" id="'.$hfIdBtn.'" style="display: none;">
                    <input type="text" placeholder="请输入内容" style="width:80%;" id="content{$data.id}">
                    <button class="mui-btn mui-btn-primary" id="'.$hfIdQueren.'">确认</button>
                </div><script type="text/javascript">mui("#'.$hfId.'")[0].addEventListener("tap",function(){$("#'.$hfIdBtn.'").toggle();});mui("#'.$hfIdQueren.'")[0].addEventListener("tap",function(){});</script>';
            }
            echo $li;
        }
    }
//    跳蚤市场回复
    public function marketHuifu($market_id =''){
        $data = Db::name('wy_market_huifu')->where('market_id',$market_id)->order('time desc')->select();
        $li = '';
        if(!empty($data)){
            foreach ($data as $k => $v){
                $headimgurl = Db::name('wx_fans')->where('openid',$v['hf_openid'])->field('headimgurl')->find();
                $li .= '<li class="mui-table-view-cell mui-media">
                        <a href="javascript:;">
                            <img class="mui-media-object mui-pull-left" src="'.$headimgurl['headimgurl'].'">
                            <div class="mui-media-body">
                                <p class="mui-ellipsis" style="color: #333;">'.$v['content'].'</p>
                            </div>
                        </a>
                    </li>';
            }
            echo $li;
        }
    }

    public function leaseRental($id='',$can=''){
        $res = Db::name('rental_can')->where('id',$id)->find();
        echo $res['name'];
    }

    public function nav(){
        $http = 'http://'.$_SERVER['HTTP_HOST'];
        $nav = '<nav class="mui-bar mui-bar-tab">
    <a class="mui-tab-item" href="'.$http.'/portal/wuye/index">
        <span class="mui-icon mui-icon-home" id="home1"></span>
        <span class="mui-tab-label">首页</span>
    </a>
    <a class="mui-tab-item" href="'.$http.'/portal/wuye/service">
        <span class="mui-icon mui-icon-pengyouquan"></span>
        <span class="mui-tab-label">服务</span>
    </a>
    <a class="mui-tab-item" href="http://qiangcai.mingruisoft.com/app/index.php?i=2&c=entry&m=ewei_shopv2&do=mobile&r=quick&id=1">
        <span class="mui-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-tiaozaoshichang01"></use></svg></span>
        <span class="mui-tab-label">快捷订菜</span>
    </a>
    <a class="mui-tab-item" href="'.$http.'/portal/wuye/me">
        <span class="mui-icon mui-icon-contact"></span>
        <span class="mui-tab-label">我的</span>
    </a>
</nav>
<script>mui(\'.mui-bar\').on(\'tap\',\'a\',function(){document.location.href=this.href;});</script>';
        echo $nav;
    }
}